using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using UnityEngine;

namespace Game.Component
{
	public class VibratorItem : BaseItem
	{
		private CharacterItem character;

		private Transform targetPlatform;

		private Vector3 originalPosition;

		private bool isChangeToOriginal;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			targetPlatform = platforms[0];
			originalPosition = targetPlatform.localPosition;
		}

		protected override void LateUpdate()
		{
			if (character != null && character.IsStatic())
			{
				platforms[0].localPosition = originalPosition + new Vector3(Random.Range(-0.05f, 0.05f), Random.Range(-0.02f, 0.02f));
				isChangeToOriginal = false;
			}
			else if (!isChangeToOriginal)
			{
				targetPlatform.localPosition = originalPosition;
				isChangeToOriginal = true;
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			if (item.itemData.scriptType == 1)
			{
				character = (CharacterItem)item;
				ScannerDisplayerItem scannerDisplayerItem = (ScannerDisplayerItem)Singleton<Scene>.Instance.GetItemByScriptType(72);
				if (scannerDisplayerItem != null)
				{
					scannerDisplayerItem.IsShowDisplayer(true);
					Singleton<AudioManager>.Instance.PlayAudio("sound_vibrator", true, GetAudioPlayer());
				}
			}
			return base.AttachItem(item, depthType, attachType, updatePosition);
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			if (item == character)
			{
				character = null;
				ScannerDisplayerItem scannerDisplayerItem = (ScannerDisplayerItem)Singleton<Scene>.Instance.GetItemByScriptType(72);
				if (scannerDisplayerItem != null)
				{
					scannerDisplayerItem.IsShowDisplayer(false);
					RemoveAudioPlayer();
				}
			}
			base.ReleaseAttachItem(item);
		}
	}
}
